package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.pojo.ResultVo;
import com.usian.student.dao.StudentDao;
import com.usian.student.feign.ClassFeignService;
import com.usian.student.pojo.TbClass;
import com.usian.student.pojo.TbStudent;
import com.usian.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private ClassFeignService classFeignService;


    @Override
    public ResultVo register(TbStudent tbStudent) {


        //判断是否是一个人的姓名 手机号 身份证号
//        VerifReaName verifReaName=new VerifReaName();
//        Map result=verifReaName.checkUser(tbStudent.getCardNo(),tbStudent.getPhone(),tbStudent.getName());

//        if (result.get("code").equals("0")){
        if (true) {
            //请求接口成功
//            if (result.get("msg").equals("1")){
            if (true) {

                //判断手机号是否正确
                //验证redis 是否过期    判断用户输入的验证码是否一致
                String redisCode = stringRedisTemplate.opsForValue().get(tbStudent.getPhone());
                    if (!StrUtil.isEmpty(redisCode)) {

                        if (redisCode.equals(tbStudent.getCode())) {
                            LambdaQueryWrapper<TbStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                            lambdaQueryWrapper.eq(TbStudent::getPhone, tbStudent.getPhone());
                            long count = studentDao.selectCount(lambdaQueryWrapper);
                            if (count > 0) {
                                //手机号已经注册

                                return new ResultVo(300, "手机号已被注册", null);
                            } else {
                                //根据班级ID获取经理ID
                               ResultVo resultVo = classFeignService.info(tbStudent.getClassId());
                                TbClass tbClass = JSON.parseObject(JSON.toJSONString(resultVo.getData()),TbClass.class);
                                tbStudent.setHId(tbClass.getHid());
                                tbStudent.setHId(tbClass.getHid());
                                    //用户未注册
                                studentDao.insert(tbStudent);
                                return new ResultVo(200, "success", null);
                            }
                        } else {
                            return new ResultVo(300, "验证码错误", null);
                        }
                    } else {
                        return new ResultVo(300, "验证码过期", null);
                    }
                    //信息一致
                } else {
             //  return new ResultVo(Integer.parseInt(result.get("code")+""), (String) result.get("msg"),null);
                    return new ResultVo(300, "异常", null);

                }
            } else {
                //请求接口异常
                return new ResultVo(300, "异常", null);
//            return  new ResultVo(Integer.parseInt(result.get("code")+""),result.get("msg")+"",null);
            }

        }

}
